package at.dahu4wa.fxclient.framework;

import at.dahu4wa.fxclient.Main;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.Separator;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class MenuTreePaneView extends HBox {

	VBox buttonBox = null;
	ListView<String> list = null;
	ObservableList<String> names;
	ObservableList<IFController> items;

	public MenuTreePaneView() {
		VBox vBox = new VBox(10);

		list = new ListView<String>();
		list.getSelectionModel().selectedIndexProperty()
					.addListener(new ChangeListener<Object>() {

						@Override
					public void changed(ObservableValue<?> observable,
							Object oldValue, Object newValue) {
							System.out.println("Listview changed to " + newValue);
							if (Main.getMainController().getCurrentContent() == items.get((int) newValue).getView()){
								System.out.println("View already opened");
							} else {
								if (items.get((int) newValue).getView() == null) {
									items.get((int) newValue).init();
								}
								Main.getMainController().changeContentTo(items.get((int) newValue));
							}
						
					}
				});
		
		
		
		list.getSelectionModel().selectedItemProperty()
					.addListener(new ChangeListener<String>() {
					public void changed(ObservableValue<? extends String> ov,
							String old_val, String new_val) {
							System.out.println("Listview changed to " + new_val);
							// label.setText(new_val);
							// label.setTextFill(Color.web(new_val));
					}
				});

		/*
		 * list.getSelectionModel().selectedIndexProperty().addListener( new
		 * ChangeListener<Integer>() {
		 * 
		 * @Override public void changed(ObservableValue<? extends integer>
		 * arg0, integer arg1, integer arg2) { // TODO Auto-generated method
		 * stub
		 * 
		 * } });
		 */

		names = FXCollections.observableArrayList();
		items = FXCollections.observableArrayList();

		vBox.setPadding(new Insets(20));
		vBox.setTranslateX(5);

		Text label = new Text("Modules");
		label.setFont(Font.font("Arial", FontWeight.BOLD, 20));

		vBox.getChildren().add(label);
		vBox.getChildren().add(list);

		buttonBox = new VBox(5);

		buttonBox.setTranslateX(5);

		vBox.getChildren().add(buttonBox);

		getChildren().addAll(vBox, new Separator(Orientation.VERTICAL));
	}

	public void addButton(Button button) {
		buttonBox.getChildren().add(button);
	}

	public void addListEntry(String name, IFController controller) {
		names.add(name);
		items.add(controller);
		list.setItems(names);
	}
}
